package openbang.account.web.portal.client;


import java.util.List;

import openbang.account.service.entity.DiaoChaItem;
import openbang.account.service.entity.DiaoChaProject;
import openbang.account.service.intf.DiaoChaService;
import openbang.account.service.vo.DiaoChaItemRate;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * 显示调查项
 * 显示调查结果的百分比
 * 
 * 
 * @author aibozeng
 *
 */

public class DiaoCha implements EntryPoint {
	private static final String ProjectCode = "dc20100313";
	//根
	private VerticalPanel vPanelRoot = new VerticalPanel();
	
	HTML projTitle = new HTML();
	Grid itemList  = new Grid(4,2);
	HorizontalPanel btnPanel = new HorizontalPanel();

	Button confirmBtn = new Button("提交");
	HTML confirmTitle = new HTML("提交后看到统计结果");

	//数据
	DiaoChaProject project = null; 
	int selectedItemIndex = -1;
	
	@Override
	public void onModuleLoad() {		
		RootPanel.get("diaocha_div").add(vPanelRoot);
		vPanelRoot.add(projTitle);
		vPanelRoot.add(itemList);
		vPanelRoot.add(btnPanel);
		btnPanel.add(confirmBtn);
		btnPanel.add(new HTML("&nbsp;&nbsp;"));
		btnPanel.add(confirmTitle);
		
		projTitle.setStyleName("titleTxt");
		itemList.setStyleName("contentTxt");
		confirmBtn.setStyleName("btnTxt");
		confirmTitle.setStyleName("contentTxt");
		
		//取调查项目的信息进行显示,供用户选择
		confirmBtn.setEnabled(false);
		DiaoChaService.Util.getInstance().getProject(ProjectCode, new AsyncCallback<DiaoChaProject>(){
			public void onFailure(Throwable caught) {
				RootPanel.get("loading").setVisible(false);
				Window.alert(caught.getMessage());
				confirmBtn.setEnabled(true);
			}
			public void onSuccess(DiaoChaProject result) {
				RootPanel.get("loading").setVisible(false);
				if(result==null){
					Window.alert("获取不到调查项目的信息");
					return;
				}		
				project = result;
				initData();
				confirmBtn.setEnabled(true);
			}
		});
		
		confirmBtn.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				int count = itemList.getRowCount();
				selectedItemIndex = -1;
				for(int i=0;i<count;i++){			
					RadioButton radioButton = (RadioButton)itemList.getWidget(i,0);
					if(radioButton.getValue()){
						selectedItemIndex = i;
						break;
					}
				}
				if(selectedItemIndex<0){
					Window.alert("请选择一个选项");
					return;
				}
				confirmBtn.setEnabled(false);
				confirmTitle.setHTML("统计中...");
				DiaoChaService.Util.getInstance().addDiaoChaRecord(project.getId(), project.getItems().get(selectedItemIndex).getId(), new SubmitCallBack());
			}
		});
	}
	
	//显示调查项目问题
	private void initData(){
		projTitle.setText(project.getTitle());
		itemList.resizeRows(project.getItems().size());
		for(int i=0;i<project.getItems().size();i++){			
			DiaoChaItem item = project.getItems().get(i);
			RadioButton radioButton = new RadioButton("item",item.getTitle());
			itemList.setWidget(i,0, radioButton);
			//totalCount = totalCount + item.getCountNum();//提前计算当前所有项的总数
		}
	}
	
	//显示调查结果的百分比
	private void viewDiaoChaJieGuo(List<DiaoChaItemRate> rates){
		confirmTitle.setHTML("感谢您的参与!");
		//Window.alert(""+rates.size());
		//totalCount ++;
		//计算各项的百分比 --- 调查子项放入缓存了，本身的值已经不准
		//for(int i=0;i<project.getItems().size();i++){
		//	DiaoChaItem item = project.getItems().get(i);
		//	if(i==selectedItemIndex){
		//		item.setCountNum(item.getCountNum()+1);
		//	}
		//	item.setRate(item.getCountNum()*1.0f/(1.0f*totalCount)); 
		//}
		//显示   Grid的第二列
		int maxWidth = 60;
		int count = rates.size(); //itemList.getRowCount();
		for(int i=0;i<count;i++){
			String rate = rates.get(i).getRate();  //project.getItems().get(i).getRate();
			int theWidth = (int)( maxWidth * Float.parseFloat(rate)/100);
			String html = "<table class=\"contentTxt\"><tr><td width=\""+theWidth+"\" bgcolor=\"blue\"></td><td>"+rate+"%</td></tr>";
			itemList.setHTML(i,1,html);
		}
	}

	private class SubmitCallBack implements AsyncCallback<List<DiaoChaItemRate>>{
		@Override
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
			confirmBtn.setEnabled(true);			
		}
		@Override
		public void onSuccess(List<DiaoChaItemRate> result) {
			//confirmBtn.setEnabled(true);
			if(null==result || result.size()<=0){
				//Window.alert("");
				return;
			}
			viewDiaoChaJieGuo(result);
		}
	}
}
